package com.platform.modules.report.controller;

import com.platform.common.utils.DateUtils;
import com.platform.common.utils.MapUtil;
import com.platform.common.utils.NumberUtils;
import com.platform.modules.report.dao.QlyInsReportDao;
import com.platform.modules.report.dto.*;
import com.platform.modules.report.service.QlyInsReportService;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.dao.SysCityAreaDao;
import com.platform.modules.sys.entity.SysCityAreaEntity;
import com.platform.modules.sys.service.SysDictService;
import com.platform.web.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

@SuppressWarnings("ALL")
@Api(tags = "报表 - 质量巡查分析")
@RestController
@RequestMapping("report/qly/ins")
public class QlyInsReportController extends BaseReportController {

    @Autowired
    QlyInsReportService qlyInsReportService;
    @Autowired
    QlyInsReportDao qlyInsReportDao;
    @Autowired
    SysDictService sysDictService;
    @Autowired
    SysCityAreaDao sysCityAreaDao;


    @ApiOperation("今日巡查数据 - 巡查数q | 一般g | 较大l | 严重s")
    @PostMapping("today")
    public CommonResult<Map<String, Object>> today(@RequestBody BaseQueryDTO baseQueryDTO){

        String date = DateUtils.format(new Date());

        Integer qlyInsCount = qlyInsReportService.qlyInsCount(baseQueryDTO, MapUtil.of("date", date));
        Integer generalCount = qlyInsReportService.queCount(baseQueryDTO, MapUtil.of("date", date,"level",1));
        Integer largerCount = qlyInsReportService.queCount(baseQueryDTO, MapUtil.of("date", date,"level",2));
        Integer seriousCount = qlyInsReportService.queCount(baseQueryDTO, MapUtil.of("date", date,"level",3));

        return CommonResult.success(MapUtil.of("qlyInsCount",qlyInsCount, "generalCount",generalCount, "largerCount",largerCount, "seriousCount",seriousCount));
    }



    @ApiOperation("各市质量巡查次数排名 - 默认:表格")
    @PostMapping("qlyInsCountGroupByCity")
    public CommonResult<List<QlyInsDistributeDTO>> qlyInsCountGroupByCity(@RequestBody BaseQueryDTO baseQueryDTO){
        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());
        Map<String,Object> qlyInsCountMap = qlyInsReportService.qlyInsCountGroupByCity(baseQueryDTO);
        List<QlyInsDistributeDTO> data = new ArrayList<>(areas.size());
        areas.forEach(e->{
            String xzqh = e.getId();
            Integer qlyInsCount = NumberUtils.intValueOf0(qlyInsCountMap.get(xzqh));
            data.add(new QlyInsDistributeDTO(xzqh, e.getName(), qlyInsCount));
        });
        return CommonResult.success(data);
    }

    @ApiOperation("各市超期未整改排名 - 默认:表格")
    @PostMapping("cityOverdueRanking")
    public CommonResult<List<QlyInsOverdueDTO>> cityOverdueRanking(@RequestBody BaseQueryDTO baseQueryDTO){

        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());

        Map<String,Object> overDueCountMap = qlyInsReportService.overdueCountGroupByCity(baseQueryDTO);
        Map<String,Object> avgOverDueDaysMap = qlyInsReportService.avgOverdueDaysGroupByCity(baseQueryDTO);

        List<QlyInsOverdueDTO> data = new ArrayList<>(areas.size());
        DecimalFormat df = new DecimalFormat("0.00");
        areas.forEach(e->{
            String xzqh = e.getId();
            Integer overDueCount = NumberUtils.intValueOf0(overDueCountMap.get(xzqh));
            Integer avgOverDueDays = ((BigDecimal)avgOverDueDaysMap.getOrDefault(xzqh,BigDecimal.ZERO)).intValue();
            data.add(new QlyInsOverdueDTO(xzqh,e.getName(), overDueCount, avgOverDueDays));
        });

        return CommonResult.success(data);
    }

//    @ApiOperation("施工单位超期未整改排名 - 默认:表格")
//    @PostMapping("sgdwOverdueRanking")
//    public CommonResult<List<QlyInsOverdueDTO>> sgdwOverdueRanking(@RequestBody BaseQueryDTO baseQueryDTO){
//        List<SysCityAreaEntity> list = sysCityAreaDao.selectList(MapUtil.of("pid",baseQueryDTO.getProvinceNum()));
//        List<Map<String,Object>> overDueInfoList = qlyInsReportDao.overDueInfoGroupByCity(baseQueryDTO);
//        Map<String,Object> overDueCountMap = MapUtil.flat(overDueInfoList,"CITY_NUM","COUNT");
//        Map<String,Object> avgOverDueDaysMap = MapUtil.flat(overDueInfoList,"CITY_NUM","AVG_OVERDUE_DAYS");
//
//        List<QlyInsOverdueDTO> data = new ArrayList<>(list.size());
//        DecimalFormat df = new DecimalFormat("0.00");
//        list.forEach(e->{
//            String xzqh = e.getId();
//            Integer overDueCount = NumberUtils.intValueOf0(overDueCountMap.get(xzqh));
//            Integer avgOverDueDays = ((BigDecimal)avgOverDueDaysMap.getOrDefault(xzqh,BigDecimal.ZERO)).intValue();
//            data.add(new QlyInsOverdueDTO(xzqh,e.getName(), overDueCount, avgOverDueDays));
//        });
//
//        return CommonResult.success(data);
//    }


    @ApiOperation("质量问题等级占比 - 默认:饼图")
    @PostMapping("queCountGroupByLevel")
    public CommonResult<List<BaseItemDTO>> queCountGroupByLevel(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> queCountMap = qlyInsReportService.queCountGroupByLevel(baseQueryDTO);
        List<BaseItemDTO> list = Arrays.asList(
                new BaseItemDTO("严重",queCountMap.getOrDefault("3",0),"3"),
                new BaseItemDTO("较大",queCountMap.getOrDefault("2",0),"2"),
                new BaseItemDTO("一般",queCountMap.getOrDefault("1",0),"1")
        );
        return CommonResult.success(list);
    }

    @ApiOperation("质量问题类型占比 - 默认:饼图")
    @PostMapping("queCountGroupByType")
    public CommonResult<List<BaseItemDTO>> queCountGroupByType(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> queCountMap = qlyInsReportService.queCountGroupByType(baseQueryDTO);
        List<BaseItemDTO> list = Arrays.asList(
                new BaseItemDTO("给排水工程",queCountMap.getOrDefault("1",0),"1"),
                new BaseItemDTO("防水工程",queCountMap.getOrDefault("2",0),"2"),
                new BaseItemDTO("砌筑工程",queCountMap.getOrDefault("3",0),"3"),
                new BaseItemDTO("装饰装修工程",queCountMap.getOrDefault("4",0),"4"),
                new BaseItemDTO("钢筋工程",queCountMap.getOrDefault("5",0),"5"),
                new BaseItemDTO("模版工程",queCountMap.getOrDefault("6",0),"6"),
                new BaseItemDTO("混凝土工程",queCountMap.getOrDefault("7",0),"7")
        );
        return CommonResult.success(list);
    }

    @ApiOperation("隐患状态等级占比 - 默认:饼图")
    @PostMapping("qlyInsCountGroupByStatus")
    public CommonResult<List<BaseItemDTO>> qlyInsCountGroupByStatus(@RequestBody BaseQueryDTO baseQueryDTO){
        Map<String,Object> queCountMap = qlyInsReportService.qlyInsCountGroupByStatus(baseQueryDTO);
        List<BaseItemDTO> data = Arrays.asList(
                new BaseItemDTO("待整改",queCountMap.getOrDefault("1",0),"1"),
                new BaseItemDTO("待复查",queCountMap.getOrDefault("2",0),"2"),
                new BaseItemDTO("超期未整改",queCountMap.getOrDefault("4",0),"4"),
                new BaseItemDTO("已完成",queCountMap.getOrDefault("3",0),"3")
        );
        return CommonResult.success(data);
    }

}
